package com.iris.live.services.covervo;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by liuweilong on 2017/9/14.
 * 实体类转换成Map集合
 */
public class DaoFromMap {
    public static Map getMap(Object thisObj) {
        Map map = new HashMap();
        Class c;
        try {
            c = Class.forName(thisObj.getClass().getName());
            Method[] m = c.getMethods();
            for (int i = 0; i < m.length; i++) {
                String method = m[i].getName();
                if (method.startsWith("get")) {
                    try {
                        Object value = m[i].invoke(thisObj);
                        if (value != null && !"getClass".equals(method)) {
                            String key = method.substring(3);
                            key = key.substring(0, 1).toUpperCase()+ key.substring(1);
                            map.put(method.substring(3).toLowerCase(), value);
                        }
                    } catch (Exception e) {
                        System.out.println("error:" + method);
                    }
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return map;
    }
}
